package com.ctcemti.mdm.project.sys.controller;


import com.ctcemti.mdm.common.result.Result;
import com.ctcemti.mdm.common.result.ResultEnum;
import com.ctcemti.mdm.common.result.ResultUtil;
import com.ctcemti.mdm.common.util.ExcelParseUtils;
import com.ctcemti.mdm.common.util.JWTUtil;
import com.ctcemti.mdm.common.util.StringUtils;
import com.ctcemti.mdm.project.sys.entity.OrgProject;
import com.ctcemti.mdm.project.sys.service.ICompanyService;
import com.ctcemti.mdm.project.sys.service.IProjectService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@RestController
@Api(value = "ProjectController", description = "项目控制器类")
@RequestMapping("project")
public class ProjectController {

    @Autowired
    IProjectService projectService;

    @Autowired
    ICompanyService companyService;



    @PostMapping("insertOrUpdate")
    public Result insertCompany(HttpServletRequest request,OrgProject orgProject){
        String loginId = JWTUtil.getLoginId(request.getHeader("Authorization"));
        String loginName = JWTUtil.getLoginName(request.getHeader("Authorization"));
        orgProject.setCreator(loginId);
        orgProject.setModifiedBy(loginId);
        orgProject.setModifyDate(new Date());
        Result result = projectService.insertOrUpdate(orgProject);
        result.setRefreshToken(request);
        return result;
    }

    @PostMapping("moveTo")
    public Result moveTo(HttpServletRequest request,String orgId,String id){
        Result result = projectService.moveTo(orgId,id);
        result.setRefreshToken(request);
        return result;
    }

    @PostMapping("getAllProjects")
    public Result getAllProjects(HttpServletRequest request,Integer page,Integer limit,OrgProject entity){
        return new Result(ResultEnum.SUCCESS,projectService.getAllProjects(page,limit,entity));
    }

    @PostMapping("importPros")
    public Result importPros(HttpServletRequest request,@RequestParam("file") MultipartFile file,String orgId){
        try{
            List<OrgProject> list = ExcelParseUtils.importExcel(file,0,1,OrgProject.class);
            for(OrgProject o:list){
                o.setOrgId(orgId);
                projectService.insertOrUpdate(o);
            }
        }catch(Exception e){
            return ResultUtil.error(ResultEnum.ZTSJ_1021);
        }
        return new Result(ResultEnum.SUCCESS,"导入成功");
    }


    @PostMapping("delete")
    public Result delete(HttpServletRequest request,String ids,String orgId){
        if(StringUtils.isNullOrEmpty(ids)){
            return ResultUtil.error(ResultEnum.ZTSJ_1008);
        }
        String[] idList = ids.split(",");
        if(idList!=null && idList.length>0){
            for(String id:idList){
                if(!id.equals("")){
                    OrgProject orgProject = new OrgProject();
                    orgProject.setId(id);
                    projectService.delete(orgProject);
                }
            }
        }
        return new Result(ResultEnum.SUCCESS,null);
    }


    @PostMapping("getByPid")
    public Result<PageInfo<OrgProject>> getByPersonId(String companyId,Integer page,Integer limit){
        if(StringUtils.isNullOrEmpty(companyId)){
            return ResultUtil.success();
        }
        PageInfo<OrgProject> orgProjectList = projectService.getProjectsByPid(companyId,page,limit);
        return ResultUtil.success(orgProjectList);
    }

    @PostMapping("selectProByUserId")
    public Result selectProByUserId(HttpServletRequest request,String loginId){
        String loginid = JWTUtil.getLoginId(request.getHeader("Authorization"));
        String loginName = JWTUtil.getLoginName(request.getHeader("Authorization"));
        if(StringUtils.isNullOrEmpty(loginid)){
            return new Result(ResultEnum.ZTSJ_1011,null);
        }
        List<OrgProject> orgProjects = projectService.selectProByUserId(request.getHeader("Authorization"));
        return new Result(ResultEnum.SUCCESS,orgProjects);
    }

    @PostMapping("getProjectsForTree")
    public Result getProjectsForTree(HttpServletRequest request,String id, String type, String personId){
        String loginId = JWTUtil.getLoginId(request.getHeader("Authorization"));
        personId = loginId;
        if(StringUtils.isNullOrEmpty(id)){
            return ResultUtil.success();
        }
        if(StringUtils.isNullOrEmpty(type)){
            return ResultUtil.success();
        }
        if(StringUtils.isNullOrEmpty(personId)){
            return ResultUtil.success();
        }
        return projectService.getProjectsForTree(id,type,personId);
    }

    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request){
        //转换日期  
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class,new CustomDateEditor(dateFormat,true));// CustomDateEditor为自定义日期编辑器  
    }



}
